######################################################################
#
# wt::contextInfo package
#
######################################################################

package require wt::server
package require wt::page
package require wt::request
package require wt::response
package require wt::util

namespace eval ::wt::contextInfo:: {
  namespace export printContext
}

proc ::wt::contextInfo::printContext {} {
  namespace import ::wt::server::*
  namespace import ::wt::page::*
  namespace import ::wt::request::*
  namespace import ::wt::response::*

  set buf ""

  #append buf "<center>"

  append buf "<style type=\"text/css\">"
  append buf "h2.c {font-family: arial, sans-serif, verdana, helvetica;} "
  append buf "h4.c {font-family: arial, sans-serif, verdana, helvetica;} "
  append buf "p.c {font-family: arial, sans-serif, verdana, helvetica;} "
  append buf "td.c {font-family: arial, sans-serif, verdana, helvetica;} "
  append buf "pre.c {font-family: arial, sans-serif, verdana, helvetica;} "
  append buf "</style>"

  set title "Request Settings"
  set table [taskSettings items]
  append buf [printValTable $table $title]

  set title "Server Variables"
  set table [serverVars items]
  append buf [printValTable $table $title]

  set title "Request Headers"
  set table [requestHeaders items]
  append buf [printValTable $table $title 1]

  set title "Query String Parameters"
  set table [queryParams items]
  append buf [printValTable $table $title 1]

  set title "Posted Parameters"
  set table [postParams items]
  append buf [printValTable $table $title 1]

  set title "Request Cookies"
  set table [cookies items]
  append buf [printValTable $table $title 1]

  set title "Uploaded Files"
  set table [table create]
  foreach key [uploads keys] {
    set list [uploads getAll $key]
    foreach upload $list {
      table add $key [upload $upload table]
    }
  }
  append buf [printValTable $table $title 1]

  #set title "Session"
  #set table [wt::session::session table]
  #append buf [printValTable $table $title]

  set title "Response Headers"
  set table [headers items]
  append buf [printValTable $table $title 1]

  set title "Non-Error Headers"
  set table [successHeaders items]
  append buf [printValTable $table $title 1]

  set title "Response Cookies"
  set table [table create]
  foreach key [responseCookies keys] {
    set list [responseCookies getAll $key]
    foreach cookie $list {
      table add $key [cookie $cookie table]
    }
  }
  append buf [printValTable $table $title 1]

  set title "Worker Information"
  set table [workerInfo]
  append buf [printValTable $table $title]

  set title "Task Information"
  set table [taskInfo]
  append buf [printValTable $table $title]

  set title "Page Loader Information"
  set table [loaderInfo]
  append buf [printValTable $table $title]

  set pages [wt::page::pageStack]
  set size [llength $pages]
  set countStr "("
  if {$size == 1} {
    append countStr "1 item"
  } else {
    append countStr "$size items"
  }
  append countStr ")"

  append buf "<h4 class=c>Include Stack $countStr</h4>"

  for {set i 0} {$i < [llength $pages]} {incr i} {
    set num [expr $i + 1]
    append buf "<p class=c><b>Page #$num</b></p>";
    set page [lindex $pages $i]
    append buf "<table cellpadding=5 cellspacing=0 border=0 width=100% style=\"border: 1px solid black;\">"
    append buf "<tr valign=top><td class=c width=225><b>Requested path</b></td>"
    append buf "<td class=c>[table get $page requestedPath]</td></tr>"
    append buf "<tr valign=top><td class=c width=225><b>Actual path</b></td>"
    append buf "<td class=c>[table get $page targetPath]</td></tr>"
    append buf "<tr valign=top><td class=c width=225><b>Parsed source</b></td>"
    set str [wt::util::previewString [table get $page parsed]]
    append buf "<td class=c>$str</td></tr>"
    append buf "<tr valign=top><td class=c width=225><b>State</b></td>"
    append buf "<td class=c>[table get $page state]</td></tr>"

    append buf "</tr></table>"
  }

  append buf "<h4 class=c>Request Time</h4>"

  append buf "<table cellpadding=5 cellspacing=0 border=0 width=100% style=\"border: 1px solid black;\">"
  append buf "<tr><td class=c width=225><b>Request time</b></td>"
  append buf "<td class=c>[clock format [unixRequestTime]]</td></tr>"
  append buf "<tr><td class=c width=225><b>Current elapsed time</b></td>"
  append buf "<td class=c>[expr [elapsedTime] / 1000.0]</td></tr>"
  append buf "</table>"

  #append buf "</center>"

  return $buf
}

proc ::wt::contextInfo::printValTable {table title {multiVals 0} {htmlEncode 1}} {
  set buf ""

  set size [table count $table]

  set countStr "("
  if {$size == 1} {
    append countStr "1 item"
  } else {
    append countStr "$size items"
  }
  append countStr ")"

  append buf "<h4 class=c>[wt::web::htmlEncode $title] "
  append buf "[wt::web::htmlEncode $countStr]</h4>"

  if {$size == 0} {
    return $buf
  }

  append buf "<table cellpadding=5 cellspacing=0 border=0 width=100% style=\"border: 1px solid black;\">"

  set keys [lsort [table keys $table]]

  foreach key $keys {
    append buf "<tr valign=top>"
    append buf "<td width=\"225\" class=c><b>[wt::web::htmlEncode $key]</b></td>"
    append buf "<td class=c>"
    if {$multiVals} {
      set list [table get $table $key]
      for {set i 0} {$i < [llength $list]} {incr i} {
        append buf [wt::util::previewString [lindex $list $i] 200 $htmlEncode]
        if {$i < [expr [llength $list] - 1]} {
          append buf "<b>,</b><br>"
        }
      }
    } else {
      set str [table get $table $key]
      append buf [wt::util::previewString $str 200 $htmlEncode]
    }
    append buf "</td></tr>"
  }

  append buf "</table>"

  return $buf
}

package provide wt::contextInfo 1
